package com.foxit.adapp;
import java.util.ArrayList;
import java.util.Random;
import java.util.Arrays;

/*
import javax.servlet.*;
import java.io.*;
import java.net.*;
import java.lang.*;
*/

public class AdidInfo {
 //   PrintWriter out;
    private String adrule;
    private String adid;
    private String advertiser;
    private String related;
    private String lang;
    private String server;
    private String udid;
    private String redirect;
    private String nation;
    private String show_mode;
    private int group;
    private int groupno;
    private int adtype;
    private String dimension;
    private String path;
    private String adurl;
    private String adimg;

    public AdidInfo() {
    }
    public AdidInfo(String adrule,String lang,String server,String udid,String redirect,String nation,String show_mode,int group,int groupno,int adtype) {
          this.adrule = adrule;
          this.lang = lang;
          this.server = server;
          this.udid = udid;
          this.redirect = redirect;
          this.nation = nation;
          this.show_mode = show_mode;
          this.group = group;
          this.groupno = groupno;
          this.adtype = adtype;
    }

    public void setADRule(String adrule) {
          this.adrule = adrule;
    }
    public void setLang(String lang) {
          this.lang = lang;
    }
    public void setServer(String server) {
          this.server = server;
    }
    public void setUdid(String udid) {
          this.udid = udid;
    }
    public void setRedirect(String redirect) {
          this.redirect = redirect;
    }
    public void setNation(String nation) {
          this.nation = nation;
    }
    public void setShowMode(String show_mode) {
          this.show_mode = show_mode;
    }
    public void setGroup(int group) {
          this.group = group;
    }
    public void setGroupno(int groupno) {
          this.groupno = groupno;
    }
    public void setAdtype(int adtype) {
          this.adtype = adtype;
    }
    public void setRelated(String related ) {
          this.related = related;
    }
    public String getRelated() {
          return this.related;
    }
    public void setIdInfo(String adset,String lang) {
          if(adset == null) return;
          String[] id=adset.split(";");
          int i;
//this.out.println("id.length:"+id.length+" lang:"+lang);
          if(id.length == 2) {
             String[] ar=id[0].split(":");
             if(ar.length == 2) {
                this.advertiser = ar[0];
                this.related = ar[1];
             }
             String[] a= id[1].split(",");
             this.adid = a[1];
             this.adimg = a[2];
             this.adurl = a[3];
          } else if(id.length > 2) {
             String[] ar=id[0].split(":");
             if(ar.length == 2) {
                this.advertiser = ar[0];
                this.related = ar[1];
             }
             ArrayList<String[]> ad=new ArrayList<String[]>();
             for(i=1;i < id.length;i++) {
                String[] a= id[i].split(",");
                ad.add(a);
             }
             for(i=0;i < ad.size();i++) {
                if(ad.get(i)[0].equals(lang)) {
                   this.adid = ad.get(i)[1];
                   this.adimg = ad.get(i)[2];
                   this.adurl = ad.get(i)[3];
                   break;
                }
             }
             if(i == ad.size()) {
                for(i=0;i < ad.size();i++) {
                   if(ad.get(i)[0].equals("en")) {
                      this.adid = ad.get(i)[1];
                      this.adimg = ad.get(i)[2];
                      this.adurl = ad.get(i)[3];
                      break;
                   }
                }
             }
             if(i == ad.size()) {
                this.adid = ad.get(0)[1];
                this.adimg = ad.get(0)[2];
                this.adurl = ad.get(0)[3];
             }
          }
//this.out.println("adimg:"+this.adimg);
    }
    public boolean makePath(int i) {
          if(i == 1) {
             this.path = server+"/"+advertiser+"/"+udid+"/"+nation+"/"+show_mode+"/"+group+"_"+groupno+"_"+adtype+"/"+lang+"/"+adrule+"/"+adid+"/"+dimension;
          } else {
             this.path = server+"/"+advertiser+"/"+udid+"/"+nation+"/"+show_mode+"/"+group+"_"+groupno+"_"+adtype+"/"+lang+"/"+adrule+"/"+adid;
          }
          return true;
    }
    public boolean setInfoData(String adrule,int adtype,String adset,String lang) {
          this.adrule=adrule;
          this.adtype = adtype;
          this.lang = lang;
          setIdInfo(adset,lang);
          return makePath(0);
    }
    public boolean setInfoData(String adrule,int adtype,String adset,String lang,String dimension) {
          this.adrule=adrule;
          this.adtype = adtype;
          this.lang = lang;
          setIdInfo(adset,lang);
          this.dimension = dimension;
          return makePath(1);
    }
    public String getRedPath() {
          return "http://"+redirect+"/"+path+"*"+adurl;
    }
    public String getImgPath() {
          return adimg+"?"+path;
    }
}
